package com.common.web.dataset.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ObjectUtils {

	private static final Logger LOG = LoggerFactory.getLogger(ObjectUtils.class);

	/**
	 * 反射取得bean中对应属性的值
	 * 
	 * @param bean
	 * @param fieldName
	 * @return
	 */
	public static Object getProperty(Object bean, String fieldName) {
		Object obj = getProperty(bean.getClass(), bean, fieldName);
		return obj;
	}
	/**
	 * 反射获取bean中对应的属性
	 * @param bean
	 * @param fieldName
	 * @return
	 */
	public static Field getField(Object bean, String fieldName) {
		Field obj = getField(bean.getClass(), bean, fieldName);
		return obj;
	}

	/**
	 * 反射获取bean中对应的declared的方法
	 * @param bean
	 * @param methodName
	 * @return
	 */
	public static Method getMethod(Object bean,String methodName){
		Method method = getMethod(bean.getClass(), bean, methodName);
		return method;
	}
	
	private static Object getProperty(Class<?> _class, Object bean, String fieldName) {
		Object obj = null;
		Field[] fields = _class.getDeclaredFields();
		Field.setAccessible(fields, true);
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			if (fieldName.equals(field.getName())) {
				try {
					obj = field.get(bean);
				} catch (IllegalArgumentException e) {
					LOG.error(e.getMessage());
				} catch (IllegalAccessException e) {
					LOG.error(e.getMessage());
				}
				break;
			}
		}
		if (obj == null && _class.getGenericSuperclass() != null) {
			obj = getProperty(_class.getSuperclass(), bean, fieldName);
		}
		return obj;
	}
	private static Field getField(Class<?> _class, Object bean, String fieldName) {
		Field mField = null;
		Field[] fields = _class.getDeclaredFields();
		Field.setAccessible(fields, true);
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			if (fieldName.equals(field.getName())) {
				mField = field;
				break;
			}
		}
		if (mField == null && _class.getGenericSuperclass() != null) {
			mField = getField(_class.getSuperclass(), bean, fieldName);
		}
		return mField;
	}
	
	private static Method getMethod(Class<?> _class, Object bean, String methodName) {
		Method mMethod = null;
		Method[] methods = _class.getDeclaredMethods();
		Method.setAccessible(methods, true);
		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			if (methodName.equals(method.getName())) {
				mMethod = method;
				break;
			}
		}
		if (mMethod == null && _class.getGenericSuperclass() != null) {
			mMethod = getMethod(_class.getSuperclass(), bean, methodName);
		}
		return mMethod;
	}
	
	public static void main(String[] args) {
//		DataFieldDescription description = new DataFieldDescription();
//		Field field = getField(description, "text");
//		field.setAccessible(true);
//		try {
//			field.set(description, "text content");
//			System.out.println(description);
//		} catch (IllegalArgumentException e) {
//			e.printStackTrace();
//		} catch (IllegalAccessException e) {
//			e.printStackTrace();
//		}
		
		
//		Pbs pbs = new Pbs();
//		pbs.setKey("123");
//		pbs.setName("刘杰");
//		Method method = getMethod(pbs, "toName");
//		try {
//			Object object = method.invoke(pbs, null);
//			System.out.println(object);
//		} catch (IllegalArgumentException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IllegalAccessException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (InvocationTargetException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
	}
}